<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="应用名称" prop="title">
                    <el-input class="w-[280px]" v-model="queryParams.title" />
                </el-form-item>
                <el-form-item label="应用包名" prop="package_name">
                    <el-input class="w-[280px]" v-model="queryParams.package_name" />
                </el-form-item>
                <el-form-item label="应用状态" prop="status">
                    <el-select
                        v-model="queryParams.status"
                        class="w-[280px]"
                        clearable
                    >
                        <el-option label="全部" value="-1" />
                        <el-option label="启用" value="1" />
                        <el-option label="禁用" value="0" />

                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button v-perms="['client:configs:add']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>
            <el-table
                class="mt-4"
                size="large"
                v-loading="pager.loading"
                :data="pager.lists"
            >
                <el-table-column label="应用编号" prop="id" min-width="80" />
                <el-table-column label="应用名称" show-overflow-tooltip prop="title" min-width="100" />
                <el-table-column label="应用标识" show-overflow-tooltip prop="package_name" min-width="100" />
                <el-table-column label="唯一标识" show-overflow-tooltip prop="app_id" min-width="100" />
                <el-table-column label="版本号" prop="version" min-width="100" />
                <el-table-column label="支付渠道标识" prop="pay_flags" min-width="200">
                        <template #default="{ row }">
                            <el-select class="w-[280px]" multiple tag-type="success" disabled  v-model="row.pay_flags">
                                <el-option
                                    v-for="item in optionsData.articleCate"
                                    :key="item.flag"
                                    :label="item.title"
                                    :value="item.flag"
                                />
                            </el-select>
                        </template>

                </el-table-column>
                <el-table-column label="应用状态" prop="status" min-width="100">
                    <template #default="{ row }">
                        <el-tag :type="row.status === 1 ? 'success' : 'danger'">
                            {{ row.status === 1 ? '启用' : '禁用' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" prop="create_time" min-width="100" />
                <el-table-column label="更新时间" prop="update_time" min-width="100" />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['client:app_configs:edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="['client:app_configs:del']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            @success="getLists"
            @close="showEdit = false"
        />
    </div>
</template>
<script lang="ts" setup name="configs">
import { configsDelete, configsLists } from '@/api/client/app_configs'
import { configsAll } from '@/api/client/pay_configs'
import { useDictOptions } from '@/hooks/useDictOptions'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    title: '',
    package_name: '',
    app_id: '',
    status: '-1',
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: configsLists,
    params: queryParams
})


const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await configsDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

// 存储支付配置数据
const { optionsData } = useDictOptions<{
    articleCate: any[]
}>({
    articleCate: {
        api: configsAll
    }
})

onMounted(() => {
    getLists();
});
</script>
